﻿using System.Diagnostics;
using System.Linq;

namespace StoCombatLogHUD.Parsing
{
	/// <summary>
	/// Useful extensions for CombatLogEntry.
	/// </summary>
	public static class CombatLogEntryExtensions
	{
		/// <summary>
		/// Supported CombatLogEntry types.
		/// </summary>
		public enum CombatLogEntryType
		{
			ShieldHealing,
			HullHealing,
			ShieldDamage,
			HullDamage
		}

		/// <summary>
		/// Gets the type of the combat log entry.
		/// </summary>
		/// <param name="combatLogEntry">The combat log entry.</param>
		/// <returns></returns>
		public static CombatLogEntryType GetCombatLogEntryType(this CombatLogEntry combatLogEntry)
		{
			if ((int) combatLogEntry.BaseMagnitude == 0)
			{
				if (combatLogEntry.Type == "Shield")
					return CombatLogEntryType.ShieldHealing;

				if (combatLogEntry.Type == "HitPoints")
					return CombatLogEntryType.HullHealing;
			}

			if (combatLogEntry.Type == "Shield")
				return CombatLogEntryType.ShieldDamage;

			return CombatLogEntryType.HullDamage;
		}

		/// <summary>
		/// Determines whether this is an entry for a pet.
		/// </summary>
		/// <param name="combatLogEntry">The combat log entry.</param>
		/// <returns>
		///   <c>true</c> if this is a pet entry; otherwise, <c>false</c>.
		/// </returns>
		public static bool IsPetEntry(this CombatLogEntry combatLogEntry)
		{
			var petNames = new[]
				{
					"_Pet_",
					"_Pet]",
					"_Satellite_",
					"_Distress_Call_",
					"_Photonic_Fleet_",
				};

			return petNames.Any(petName => combatLogEntry.Source.InternalName.Contains(petName));

			//if (combatLogEntry.Source.InternalName != "*" &&
			//		combatLogEntry.Owner.InternalName != combatLogEntry.Target.InternalName)
			//{
			//	Debug.WriteLine(combatLogEntry.Source.InternalName);
			//}
			//return false;
		}

		/// <summary>
		/// Gets the effective value.
		/// </summary>
		/// <param name="combatLogEntry">The combat log entry.</param>
		/// <returns></returns>
		public static double GetEffectiveValue(this CombatLogEntry combatLogEntry)
		{
			return combatLogEntry.Magnitute < 0
				       ? -combatLogEntry.Magnitute
				       : combatLogEntry.Magnitute;
		}

		/// <summary>
		/// Gets the effective damage reduction.
		/// </summary>
		/// <param name="combatLogEntry">The combat log entry.</param>
		/// <returns></returns>
		public static double GetEffectiveDamageReduction(this CombatLogEntry combatLogEntry)
		{
			return ((((int) combatLogEntry.Magnitute) == 0) || (((int) combatLogEntry.BaseMagnitude) == 0))
				       ? 0
				       : (1.0 - (combatLogEntry.BaseMagnitude/combatLogEntry.Magnitute));
		}
	}
}
